//
//  Noti.swift
//  Runner
//
//  Created by topnet on 2023/5/26.
//

import UIKit
import umeng_push_sdk

class TPNoti: NSObject {

    static let shared = TPNoti()
    override init() {
        super.init()
        UNUserNotificationCenter.current().delegate = self
    }
}

extension TPNoti: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo;
        UmengPushSdkPlugin.didReceiveUMessage(userInfo)
        if notification.request.trigger?.isKind(of: UNPushNotificationTrigger.classForCoder()) == true {
            //应用处于后台时的远程推送接受
            //必须加这句代码
            UMessage.setAutoAlert(true)
            UMessage.didReceiveRemoteNotification(userInfo)
        }else {
            //应用处于前台时的本地推送接受
        }
        // 控制推送消息是否直接在前台显示
        completionHandler([.sound, .badge, .alert]);
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let notification = response.notification
        let userInfo = notification.request.content.userInfo;
        UmengPushSdkPlugin.didOpenUMessage(userInfo)
        if notification.request.trigger?.isKind(of: UNPushNotificationTrigger.classForCoder()) == true {
            //应用处于后台时的远程推送接受
            //必须加这句代码
            UMessage.didReceiveRemoteNotification(userInfo)
        }else {
            //应用处于后台时的本地推送接受
        }
    }
}
